package com.qc.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusInportDao;
import com.qc.bus.entity.BusGoods;
import com.qc.bus.entity.BusInport;
import com.qc.bus.entity.BusProvider;
import com.qc.bus.service.BusGoodsService;
import com.qc.bus.service.BusInportService;
import com.qc.bus.service.BusProviderService;
import com.qc.bus.vo.InportVo;
import com.qc.sys.common.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * (BusInport)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-09 19:33:57
 */
@Service
public class BusInportServiceImpl extends ServiceImpl<BusInportDao, BusInport> implements BusInportService {

    @Autowired
    private BusInportDao inportDao;

    @Autowired
    private BusProviderService providerService;

    @Autowired
    private BusGoodsService goodsService;

    @Override
    public DataGridView queryAllInport(InportVo inportVo) {
        IPage<BusInport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());
        QueryWrapper<BusInport> qw = new QueryWrapper<>();
        qw.eq(inportVo.getGoodsid()!=null,"goodsid",inportVo.getGoodsid());
        qw.eq(inportVo.getProviderid()!=null,"providerid",inportVo.getProviderid());
        qw.ge(inportVo.getStartTime()!=null,"inporttime",inportVo.getStartTime());
        qw.le(inportVo.getEndTime()!=null,"inporttime",inportVo.getEndTime());
        this.inportDao.selectPage(page,qw);
        List<BusInport> records = page.getRecords();
        for (BusInport inport : records) {
            if(null!=inport.getProviderid()){
                BusProvider provider = this.providerService.getById(inport.getProviderid());
                inport.setProvidername(provider.getProvidername());
            }
            if(null!=inport.getGoodsid()) {
                BusGoods goods = this.goodsService.getById(inport.getGoodsid());
                inport.setGoodsname(goods.getGoodsname());
                inport.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public BusInport InsertInport(BusInport inport) {
        this.inportDao.insert(inport);
        //更新库存
        Integer goodsid = inport.getGoodsid();
        //根据goodsid去查询
        BusGoods goods = this.goodsService.getById(goodsid);
        goods.setNumber(goods.getNumber()+inport.getNumber());
        this.goodsService.UpdateGoods(goods);
        return inport;
    }

    @Override
    public BusInport UpdateInport(BusInport inport) {
        //用之前的进货
        BusInport oldObj = this.inportDao.selectById(inport.getId());
        //根据商品id查询
        BusGoods goods = this.goodsService.getById(oldObj.getGoodsid());
        //1000 + oldObj 100 =  1100 goods 50 1050
        goods.setNumber(goods.getNumber()-oldObj.getNumber()+inport.getNumber());
        this.goodsService.UpdateGoods(goods);
        this.inportDao.updateById(inport);
        return inport;
    }

    @Override
    public boolean removeById(Serializable id) {
        //首先查询当前的数量
        BusInport inport = this.inportDao.selectById(id);
        //查询goods的总数量
        BusGoods goods = this.goodsService.getById(inport.getGoodsid());
        goods.setNumber(goods.getNumber()-inport.getNumber());
        this.goodsService.UpdateGoods(goods);
        return super.removeById(id);
    }
}